package FilesGenerator

import xml.{PrettyPrinter, XML}
import collection.mutable.HashMap


/**
 * Created by IntelliJ IDEA.
 * User: SDi
 * Date: 12.04.11
 * Time: 13:06
 * To change this template use File | Settings | File Templates.
 */

class ValuesFile {

   val fileName = "string.xml"
   val separator = '-'
   
   var values = List[String]()

   def addValue(name: String, value: String): Unit = {
      values = values :+ (name + separator + value)
   }

   // Creates the "string.xml" file with the entries
   def gen(path: String): Unit = {
      val file =
         <resources>
         {for (str <- values) yield
            <string name={str.split(separator).init.mkString}>{str.split(separator).last}</string>}
         </resources>


      XML.save(FileManager.createFile(path, fileName), XML.loadString(new PrettyPrinter(80, 3).format(file)), "UTF-8", true, null)
   }

   // Returns the Map corresponding to the content of string.xml
   def get(path: String): HashMap[String, String] = {

      val file = XML.loadFile(path + "/" + fileName)
      var getValues = HashMap.empty[String, String]

      for (entry <- file \\ "string") {
         getValues += (entry \\ "@name" text) -> (entry text)
      }
      getValues
   }


}